package com.mylib.libcore.mvp;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.trello.rxlifecycle2.components.support.RxFragment;
import com.umeng.message.PushAgent;


import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * BaseLazyLoadFragment懒加载
 * @param <P>
 */
public abstract class BaseLazyLoadFragment<P extends IPresenter> extends RxFragment implements IView {
    protected final String TAG = this.getClass().getSimpleName();
    private boolean isFirstLoad = false;

    protected View mRootView;

    @Nullable
    protected P mPresenter;//如果当前页面逻辑简单, Presenter 可以为 null

    private Unbinder mUnbinder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = initView(inflater, container, savedInstanceState);

        try {
            if (mRootView != null) {
                //绑定到butterknife
                mUnbinder = ButterKnife.bind(this, mRootView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        initEvent();//初始化事件

        isFirstLoad = true;//视图创建完成，将变量置为true

        if (getUserVisibleHint()) {//如果Fragment可见进行数据加载
            onLazyLoad();
            isFirstLoad = false;
        }
        //友盟统计
        PushAgent.getInstance(getContext()).onAppStart();

        return mRootView;
    }


    /**
     * 初始化 View
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    public abstract View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState);

    /**
     * 初始化数据
     *
     * @param savedInstanceState
     */
    public abstract void initData(@Nullable Bundle savedInstanceState);

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mUnbinder != null && mUnbinder != Unbinder.EMPTY)
            mUnbinder.unbind();
        this.mUnbinder = null;
        isFirstLoad = false;//视图销毁将变量置为false
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) mPresenter.onDestroy();//释放资源
        this.mPresenter = null;
        isFirstLoad = false;//视图销毁将变量置为false
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isFirstLoad && isVisibleToUser) {//视图变为可见并且是第一次加载
            onLazyLoad();
            isFirstLoad = false;
        }
    }

    //数据加载接口，留给子类实现
    public abstract void onLazyLoad();

    //初始化视图接口，子类必须实现
    public abstract View initView(LayoutInflater inflater, @Nullable ViewGroup container);

    //初始化事件接口，留给子类实现
    public abstract void initEvent();

    @Override
    public void showLoading(boolean isDialog) {
        if (isDialog) {
            if (getActivity() instanceof BaseActivity) {
                ((BaseActivity) getActivity()).showLoadingDialog();
            }
        }
    }

    @Override
    public void hideLoading() {
        if (getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).hideLoading();
        }
    }

}
